﻿﻿﻿﻿﻿﻿﻿﻿using UnityEngine;

/// <summary>
/// PlayerPref ( .plist ) 에 정보를 저장, 로드 하기 위한 클래스
/// Base64 를 이용하여 초~ 저수준으로 정보를 은닉
/// </summary>
public class PlistReadWriter
{
	// string
	public static void SetString(string szKey, string szValue)
	{
		string szEncoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(szValue));
		// Write Encoded string
		PlayerPrefs.SetString(szKey, szEncoded);
	}
	
	public static string GetString(string szKey, string szDefault = "")
	{
		// Read Encoded string
		string szEncoded = PlayerPrefs.GetString(szKey, szDefault);
		
		if(szEncoded == szDefault)
		{
			return szEncoded;
		}
			
		string szDecoded = System.Text.Encoding.UTF8.GetString( System.Convert.FromBase64String(szEncoded) );
		return szDecoded;
	}
	// int 
	public static void SetInt(string szKey, int nValue)
	{
		string szValue = nValue.ToString();
		// Write Encoded string
		SetString(szKey, szValue);
	}
	
	public static int GetInt(string szKey, int nDefault = 0)
	{
		// Read Encoded string
		string szDefault = nDefault.ToString();
		string szEncoded = PlayerPrefs.GetString(szKey, szDefault);
		
		if(szEncoded == szDefault)
		{
			return nDefault;
		}

		string szDecoded = System.Text.Encoding.UTF8.GetString( System.Convert.FromBase64String(szEncoded) );
		return int.Parse(szDecoded);
	}
	
	// float 
	public static void SetFloat(string szKey, float fValue)
	{
		string szValue = fValue.ToString();
		// Write Encoded string
		SetString(szKey, szValue);
	}
	
	public static float GetFloat(string szKey, float fDefault = 0f)
	{
		// Read Encoded string
		string szDefault = fDefault.ToString();
		string szEncoded = PlayerPrefs.GetString(szKey, szDefault);
		
		if(szEncoded == szDefault)
		{
			return fDefault;
		}
		
		string szDecoded = System.Text.Encoding.UTF8.GetString( System.Convert.FromBase64String(szEncoded) );
		return float.Parse(szDecoded);
	}

	public static void SetBoolean(string szKey, bool bValue)
	{
		string szValue = bValue.ToString();
		SetString(szKey, szValue);
	}

	public static bool GetBoolean(string szKey, bool bDefault = false)
	{
		// Read Encoded string
		string szDefault = bDefault.ToString();
		string szEncoded = PlayerPrefs.GetString(szKey, szDefault);

		if (szEncoded == szDefault)
		{
			return bDefault;
		}

		string szDecoded = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(szEncoded));
		return bool.Parse(szDecoded);
	}
}
